package com.example.gkcircle.adapter;

import android.app.Activity;
import android.graphics.Rect;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.avos.avoscloud.AVUser;
import com.example.gkcircle.App;
import com.example.gkcircle.R;
import com.example.gkcircle.bean.Movie;
import com.example.gkcircle.utils.PicUtil;
import com.example.gkcircle.utils.ToastFactory;
import com.example.gkcircle.utils.UniversalImageLoader;

import java.util.List;
import java.util.Objects;

/**
 * Created by Jehu on 16/3/29.
 */
public class MovieAdapter extends RecyclerView.Adapter<MovieAdapter.ViewHolder> implements View.OnClickListener{

    private Activity mActivity;
    private List<Movie> mData;
    private OnRecyclerViewItemClickListener mOnItemClickListener = null;

    public MovieAdapter(Activity activity, List<Movie> data){
        this.mActivity=activity;
        this.mData=data;
    }

    public static interface OnRecyclerViewItemClickListener {
        void onItemClick(View view , int position);
    }
    public void setOnItemClickListener(OnRecyclerViewItemClickListener listener) {
        this.mOnItemClickListener = listener;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View v = mActivity.getLayoutInflater().inflate(R.layout.love_movie_recycle_item, null);
        ViewHolder viewHolder=new ViewHolder(v);
        //将创建的View注册点击事件
        v.setOnClickListener(this);
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        final Movie movie=mData.get(position);
        final AVUser user=movie.getUser();
        //将数据保存在itemView的Tag中，以便点击时进行获取
        //找出holderview的实例
        if(holder instanceof ViewHolder) {
            holder.itemView.setTag(position);
            holder.content.setText(movie.getContent());
            holder.date.setText(movie.getCreatedAt());
            holder.username.setText(user.get("nickname").toString());
            //用户头像
            if(null!=user.getAVFile("avatar")){
                int avatarWidth=mActivity.getResources().getDimensionPixelSize(R.dimen.movie_avatar_width);
                //获取缩略图url
                String avatarUrl=user.getAVFile("avatar").getThumbnailUrl(true,avatarWidth,avatarWidth);
                UniversalImageLoader.getIntance().displayImage(avatarUrl,holder.avatar,UniversalImageLoader.getAvatarOptions(),UniversalImageLoader.getAnimateFirstDisplayListener());
            }else{
                holder.avatar.setImageResource(R.mipmap.personal_default_user_icon);
            }
            //标签
            String tag="";
            for(int i=0;i<movie.getTags().size();i++){
                tag+="#"+movie.getTags().get(i) +"#";
            }
            holder.tagView.setText(tag);

            //图片
            int width= (App.screenWidth- PicUtil.dip2px(mActivity,20))/ 2;
            ((ViewHolder) holder).photo.setLayoutParams(new LinearLayout.LayoutParams(width, width));
            UniversalImageLoader.getIntance().displayImage(movie.getThumbnailUrl(), holder.photo, UniversalImageLoader.getPhotorOptions(), UniversalImageLoader.getAnimateFirstDisplayListener());

            //足迹
            holder.footMarkCount.setText(movie.getViewCount() +"");
        }
    }


    @Override
    public int getItemCount() {
        return mData==null?0:mData.size();
    }

    //添加数据
    public void addItem(List<Movie> data) {
        mData.removeAll(mData);
        mData.addAll(data);
        notifyDataSetChanged();
    }

    @Override
    public void onClick(View v) {
          //使用getTag方法获取数据
        if(mOnItemClickListener!=null){
            mOnItemClickListener.onItemClick(v,(int)v.getTag());
        }
    }


    class ViewHolder extends RecyclerView.ViewHolder
    {
        ImageView avatar;
        ImageView photo;
        ImageView footMarkImg;
        TextView footMarkCount;
        TextView username;
        TextView date;
        TextView content;
        TextView tagView;

        public ViewHolder(View itemView){
            super(itemView);
            content=(TextView)itemView.findViewById(R.id.love_movie_item_content);
            photo=(ImageView)itemView.findViewById(R.id.love_movie_item_photo);
            tagView=(TextView)itemView.findViewById(R.id.love_movie_tag);
            avatar=(ImageView)itemView.findViewById(R.id.love_movie_item_avatar);
            username=(TextView)itemView.findViewById(R.id.love_movie_item_username);
            date=(TextView)itemView.findViewById(R.id.love_movie_item_date);
            footMarkCount=(TextView)itemView.findViewById(R.id.love_movie_item_footmark);
            footMarkImg=(ImageView)itemView.findViewById(R.id.love_movie_item_footmark_img);
        }

    }



    //间距
    public static class SpaceItemDecoration extends RecyclerView.ItemDecoration{

        private int top;
        private int bottom;
        private int right;
        private int left;

        public SpaceItemDecoration(int top,int bottom,int left,int right) {
            this.top = top;
            this.bottom=bottom;
            this.right=right;
            this.left=left;
        }

        @Override
        public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {

            int position=parent.getChildPosition(view);
            if(position==0 ||position==1) {
                outRect.top=top*6;
                outRect.bottom=bottom;
                outRect.left=left;
                outRect.right=right;
                return;
            }
            outRect.top=top;
            outRect.bottom=bottom;
            outRect.left=left;
            outRect.right=right;
        }
    }

}
